﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using cognysClient.CognyService;

namespace cognysClient.Admin
{
    public partial class WebForm4 : System.Web.UI.Page
    {

        //Fill the dropdown list with all the Testgroups when the page loads
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fill();
            }
        }

        //Delete Testgroup method
        protected void btnDeleteTestGroup_Click(object sender, EventArgs e)
        {
            try
            {

                Testgroup testGroup = new Testgroup();
                testGroup.ID = Convert.ToInt32(ddlEditTestGroup.SelectedItem.Value);
                testGroup.Name = tbEditTestGroup.Text;
                testGroup.Description = tbEditDescription.Text;

                using (var client = new Service1Client())
                {
                    client.RemoveTestGroup(testGroup);
                }

                Response.Write("<script>alert('Testgruppen borttagen!');</script>");
                tbEditDescription.Text = "";
                tbEditTestGroup.Text = "";
                fill();


            }
            catch
            {
                Response.Write("<script>alert('Testgruppen misslyckades att bli borttagen');</script>");
            }
        }

        //Method that fills the textboxes with the selected Testgroup information
        protected void ddlEditTestGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(ddlEditTestGroup.SelectedItem.Value);

            if (id == 0)
            {
                tbEditDescription.Text = "";
                tbEditTestGroup.Text = "";
            }
            else
            {

                var client = new CognyService.Service1Client();
                var testGroup = client.getTestGroupID(id);

                tbEditTestGroup.Text = testGroup.Name;
                tbEditDescription.Text = testGroup.Description;
            }

        }

        //Method that fills the dropdownlist
        public void fill()
        {
            var client = new CognyService.Service1Client();
            var results = (from x in client.getTestGroup()
                           select new
                           {
                               ID = x.ID,
                               TestGroupName = x.Name

                           }).ToList();

            ddlEditTestGroup.Items.Clear();
            ddlEditTestGroup.Items.Add(new ListItem("Välj Testgrupp", "0"));
            ddlEditTestGroup.DataSource = results;
            ddlEditTestGroup.DataTextField = "TestGroupName";
            ddlEditTestGroup.DataValueField = "ID";
            ddlEditTestGroup.DataBind();
        }

        //Edit Testgroup button click method
        protected void btnEditTestGroup_Click(object sender, EventArgs e)
        {
            try
            {

                Testgroup testGroup = new Testgroup();
                testGroup.ID = Convert.ToInt32(ddlEditTestGroup.SelectedItem.Value);
                var name = tbEditTestGroup.Text;
                var description = tbEditDescription.Text;
                testGroup.Name = name;
                testGroup.Description = description;

                using (var client = new Service1Client())
                {
                    client.editTestGroup(testGroup);
                }

                Response.Write("<script>alert('Uppdatering lyckad!');</script>");
                tbEditTestGroup.Text = "";
                tbEditDescription.Text = "";
                fill();


            }
            catch
            {
                Response.Write("<script>alert('Uppdatering misslyckad');</script>");
            }
        }

    }
}